<?php

/**
 * @Author: 刘洪
 * @Date:   2018-11-20 16:03:51
 * @Last Modified by:   user
 * @Last Modified time: 2019-01-04 09:48:30
 */
namespace app\common\remind;

use app\common\remind\RemindMain;
use think\Db;

/**
 * 
 * 生日提醒类
 */
class RemindBirthday extends RemindMain
{
	/**
	 * 设置提醒类型为生日提醒
	 * @var integer
	 */
	protected $remindType=1;

	/**
	 * 实例入口
	 * @return [type] [description]
	 */
	public function index(){
		$this->remindList=parent::init();

		//如果今天第一次执行提醒
		if(true == $this->is_first){

			//数据重组
			$this->regroupData();

			//发送提醒
			parent::send('生日');
		}
	}

	/**
	 * 实现父类抽象方法
	 * @access protected
	 * @return [type] [description]
	 */
	protected function regroupData()
	{
		//arr[]=[trigger,send_type,receives,send_connect]
		//		[触发类型[]，发送方式，接收人[]，发送内容[]]
		foreach ($this->remindList as $arr) {
			switch ($arr['trigger']) {
				case 0:
					if($this->is_first){


						$where="select a.name as role,c.name as department,d.user_login,d.birthday  from cmf_role as a,cmf_role_user as b,cmf_department as c,cmf_user as d where a.id=b.role_id and a.department_id=c.id and d.id=b.user_id and (   (YEAR(date_add(curdate(),interval 2 day))=YEAR(FROM_UNIXTIME((d.birthday),'%Y-%m-%d'))  and month(date_add(curdate(),interval 2 day))=month(FROM_UNIXTIME((d.birthday),'%Y-%m-%d'))  and DAY(date_add(curdate(),interval 2 day))=DAY(FROM_UNIXTIME(d.birthday,'%Y-%m-%d'))) || (YEAR(date_add(curdate(),interval 2 day))=YEAR(FROM_UNIXTIME((d.birthday),'%Y-%m-%d'))  and month(date_add(curdate(),interval 1 day))=month(FROM_UNIXTIME((d.birthday),'%Y-%m-%d'))  and DAY(date_add(curdate(),interval 1 day))=DAY(FROM_UNIXTIME(d.birthday,'%Y-%m-%d'))))  ";
					}
					break;
			}
			if(isset($where)){
				$birthday=Db::query($where);
				//格式化数据
				$this->formatSendList($birthday,$arr);
			}
		}
	}

	/**
	 * 数据格式化
	 * @access protected
	 * @param  array $birthday [description]
	 * @param  array $arr      [description]
	 * @return [type]           [description]
	 */
	protected function formatSendList($birthday,$arr)
	{
	//arr[]=[trigger,send_type,receives,send_connect]
			//[触发类型，发送方式，接收人，发送内容]
		
		foreach ($birthday as $it) {
			$sms_connect="(";
			//发送内容
			$send_connect="<div style='background-color:#ccc;margin-top:10px;'><h3>有同事要生日了：</h3>";
			
			//send_connect=[0姓名，1生日，2部门，3职位]
			foreach ($arr['send_connect'] as $k) {
				if($k==0){
					$send_connect.="<div>姓名：".$it['user_login']."</div>";
					$sms_connect.=$it['user_login']." ";
				}
				if($k==1){
					$send_connect.="<div>生日：".date("Y-m-d",$it['birthday'])."</div>";
					$sms_connect.=date("Y-m-d",$it['birthday'])." ";
				}
				if($k==2){
					$send_connect.="<div>部门：".$it['department']."</div>";
					$sms_connect.=$it['department']." ";
				}
				if($k==3){
					$send_connect.="<div>职位：".$it['role']."</div>";
					$sms_connect.=$it['role']." ";
				}
			}
			$send_connect.="</div>";
			$sms_connect.=") ";

			//接收人 
			$receives="";
			foreach ($arr['receives'] as $k) {
				$user=Db::name('user')->where('id='.$k)->find();

				//邮箱
				if($arr['send_type']==0 || $arr['send_type']==1){
					if(!isset($this->sendList['email'][$user['user_email']])){
						$this->sendList['email'][$user['user_email']]="";
					}
					$this->sendList['email'][$user['user_email']].=$send_connect;
				}

				//短信
				if($arr['send_type']==0 || $arr['send_type']==2){
					if(!isset($this->sendList['mobile'][$user['mobile']])){
						$this->sendList['mobile'][$user['mobile']]="";
					}
					$this->sendList['mobile'][$user['mobile']].=$sms_connect;
				}
			}
		}
		foreach ($this->sendList['mobile'] as $key => $value) {
				$this->sendList['mobile'][$key]=$value."，记得哦！";
		}

	}





}